package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

public class RayLiBlogAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://blog.rayli.com.cn/home[-]space[-]uid[-][0-9]*[-]do[-]blog[-]id[-][0-9]*.html",
				"http://blog.rayli.com.cn/home.php[?]mod=space&uid=[0-9]+&do=blog&id=[0-9]+",
				"http://blog.rayli.com.cn/home.php[?]uid=[0-9]+&mod=space&do=blog&id=[0-9]+",
				"http://user.rayli.com.cn/home-space-uid-[0-9]+-do-blog-id-[0-9]+.html",
				"http://space.rayli.com.cn/home-space-uid-[0-9]+-do-blog-id-[0-9]+.html",
				"http://blog.rayli.com.cn/html/[0-9]+/[0-9]+-[0-9]+.html"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet = true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		String htmltxt = urlMeta.getHtml();
		Document doc = Jsoup.parse(htmltxt);
		NewsMeta blog = new NewsMeta();
		String title = "";
		String content = "";
		Long date = null;
		Integer commentNum = 0;
		Integer clickNum = 0;
		Integer deliverNum = 0;
		String author=null;
		/**
		 * 标题
		 */
		title = doc.select("h1").text();
		Elements select = doc.select("p.xg2 > span.xg1");
		if(select.size() > 0){
			/**
			 * 时间
			 */
			date = DateUtils.matchDate(select.get(1).text());
			/**
			 * 点击数
			 */
			String string = select.get(0).text();
			if(!StringUtils.isBlank(string)){
				String click = string.replace("已有 ", "").replace(" 次阅读", "");
				try {
					clickNum = Integer.parseInt(click);
				} catch (NumberFormatException e) {
					clickNum = 0;
				}
				blog.setUpdateUrl(url);
			}
		}
		/**
		 * 内容
		 */
		content = HtmlCleaner.getContentHtml(url,  doc.select("div#blog_article"));
		author = doc.select("div.hm h2.xs2 a").text();
		blog.setAuthor(author);
		blog.setUrl(url);
		blog.setTitle(title);
		blog.setContent(content);
		blog.setDate(date);
		blog.setClickNum(clickNum);
		blog.setCommentNum(commentNum);
		//blog.setDeliverNum(deliverNum);
		blog.setType(7);
		return blog;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta != null){
			String url = meta.getUpdateUrl();
			if(!StringUtils.isBlank(url)){
				UrlMeta urlMeta = CrawlHTML.responseToURL(url);
				String htmltxt = urlMeta.getHtml();
				Document doc = Jsoup.parse(htmltxt);
				Integer clickNum = 0;
				Elements select = doc.select("p.xg2 > span.xg1");
				if(select.size() > 0){
					/**
					 * 点击数
					 */
					String string = select.get(0).text();
					if(!StringUtils.isBlank(string)){
						String click = string.replace("已有 ", "").replace(" 次阅读", "");
						try {
							clickNum = Integer.parseInt(click);
						} catch (NumberFormatException e) {
							clickNum = meta.getClickNum();
						}
						meta.setClickNum(clickNum);
						return meta;
					}
				}
			}
		}
		return null;
	}

	/**
	 * @param args
	 */
	

	
}
